﻿using System.ComponentModel;
using System.Linq;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Descriptors.Filters
{
    /// <summary>
    /// Adds a [Browsable(false)] attribute to all properties.
    /// </summary>
    public class NotBrowsablePropertyDescriptorFilter : PropertyDescriptorFilter
    {
        /// <summary>
        /// Filters the specified control.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns>A property descriptor</returns>
        public override PropertyDescriptor Filter(object instance, PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Attributes.OfType<BrowsableAttribute>().Any(browsable => browsable.Browsable == false))
                return propertyDescriptor;

            return TypeDescriptor.CreateProperty(propertyDescriptor.ComponentType, propertyDescriptor, new BrowsableAttribute(false));
        }
    }
}